
package com.jf.cloud.product.controller.multishop;

import com.alibaba.excel.EasyExcel;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.product.dto.TakeStockDTO;
import com.jf.cloud.product.dto.TakeStockSpuDTO;
import com.jf.cloud.product.listener.StockBillLogItemExcelListener;
import com.jf.cloud.product.listener.TakeStockSpuExcelListener;
import com.jf.cloud.product.model.TakeStock;
import com.jf.cloud.product.model.TakeStockSpu;
import com.jf.cloud.product.service.TakeStockService;
import com.jf.cloud.product.service.TakeStockSpuService;
import com.jf.cloud.product.vo.StockBillLogItemImportExcelVO;
import com.jf.cloud.product.vo.TakeStockSpuExcelVO;
import com.jf.cloud.product.vo.TakeStockSpuImportVO;
import com.jf.cloud.product.vo.TakeStockSpuVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 盘点商品列表
 *
 * @author zz
 * @date 2022-06-22 17:14:22
 */
@RestController("multishopTakeStockSpuController")
@RequestMapping("/m/take_stock_spu")
@Tag(name = "盘点商品列表")
public class TakeStockSpuController {

    @Autowired
    private TakeStockService takeStockService;

    @Autowired
    private TakeStockSpuService takeStockSpuService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取盘点商品列表列表" , description = "分页获取盘点商品列表列表")
    public ServerResponseEntity<PageVO<TakeStockSpu>> page(@Valid PageDTO pageDTO) {
        PageVO<TakeStockSpu> takeStockSpuPage = takeStockSpuService.page(pageDTO);
        return ServerResponseEntity.success(takeStockSpuPage);
    }

    @GetMapping("/listByTakeStockId")
    @Operation(summary = "通过takeStockId获取盘点商品列表" , description = "通过takeStockId获取盘点商品列表")
    public ServerResponseEntity<List<TakeStockSpuVO>> listByTakeStockId(Long takeStockId) {
        List<TakeStockSpuVO> takeStockSpuVOS = takeStockSpuService.listByTakeStockId(takeStockId);
        return ServerResponseEntity.success(takeStockSpuVOS);
    }

    @GetMapping
    @Operation(summary = "获取盘点商品信息" , description = "根据takeStockSpuId获取盘点商品信息")
    public ServerResponseEntity<TakeStockSpu> getByTakeStockSpuId(@RequestParam Long takeStockSpuId) {
        return ServerResponseEntity.success(takeStockSpuService.getByTakeStockSpuId(takeStockSpuId));
    }

    @PostMapping
    @Operation(summary = "保存盘点商品列表" , description = "保存盘点商品列表")
    public ServerResponseEntity<Void> save(@Valid @RequestBody TakeStockSpuDTO takeStockSpuDTO) {
        TakeStockSpu takeStockSpu = mapperFacade.map(takeStockSpuDTO, TakeStockSpu.class);
        takeStockSpu.setTakeStockSpuId(null);
        takeStockSpuService.save(takeStockSpu);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新盘点商品列表" , description = "更新盘点商品列表")
    public ServerResponseEntity<Void> update(@Valid @RequestBody TakeStockSpuDTO takeStockSpuDTO) {
        TakeStockSpu takeStockSpu = mapperFacade.map(takeStockSpuDTO, TakeStockSpu.class);
        takeStockSpuService.update(takeStockSpu);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除盘点商品列表" , description = "根据盘点商品列表id删除盘点商品列表")
    public ServerResponseEntity<Void> delete(@RequestParam Long takeStockSpuId) {
        takeStockSpuService.deleteById(takeStockSpuId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/downloadModel")
    @Operation(summary = "实物盘点商品Excel模板下载" , description = "实物盘点商品Excel模板下载")
    public void downLoadModel(HttpServletResponse response) {
        takeStockSpuService.downloadModel(response);
    }

    @Operation(summary = "导入盘点商品" , description = "导入盘点商品")
    @PostMapping("/importExcel")
//    @PreAuthorize("@pms.hasPermission('multishop:takeStockProd:import')" )
    public ServerResponseEntity<Object> importExcel(@RequestParam("excelFile") MultipartFile excelFile,
                                                    @RequestParam("takeStockId") Long takeStockId) throws Exception {
        if (Objects.isNull(excelFile)) {
            throw new LuckException("yami.network.busy");
        }
        if (Objects.isNull(takeStockId)){
            throw new LuckException("yami.supplier.not.null");
        }
        TakeStockSpuImportVO takeStockSpuImportVO = new TakeStockSpuImportVO();
        try {
            // 错误信息集合
            List<String> errorList = new ArrayList<>();
            EasyExcel.read(excelFile.getInputStream(), TakeStockSpuExcelVO.class, new TakeStockSpuExcelListener(takeStockSpuService, errorList, takeStockId, takeStockSpuImportVO)).sheet().doRead();
        } catch (IOException e) {
            throw new LuckException(e.getMessage());
        }
        return ServerResponseEntity.success(takeStockSpuImportVO);
    }

    @GetMapping("/exportTakeStockSpu")
    @Operation(summary = "实物盘点商品Excel导出" , description = "实物盘点Excel导出")
//    @PreAuthorize("@pms.hasPermission('multishop:takeStock:export')" )
    public void exportTakeStock(Long takeStockId, HttpServletResponse response) {
        if (takeStockId == null) {
            throw new LuckException("盘点id不能为空");
        }
        TakeStock takeStock = takeStockService.getByTakeStockId(takeStockId);
        if (takeStock == null) {
            throw new LuckException("实物盘点不存在");
        }
        takeStockSpuService.exportExcel(takeStockId, response);
    }

}
